package com.csdj.t309springboot.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 在英语中，我们有一个叫做 词根(root)的概念，它可以跟着其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如，词根an，跟随着单词 other(其他)，可以形成新的单词 another(另一个)。
 *
 * 现在，给定一个由许多词根组成的词典和一个句子。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根，则用最短的词根替换它。
 *
 * 你需要输出替换之后的句子。
 *
 *  
 *
 * 示例 1：
 *
 * 输入：dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
 * 输出："the cat was rat by the bat"
 * 示例 2：
 *
 * 输入：dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
 * 输出："a a b c"
 * 示例 3：
 *
 * 输入：dictionary = ["a", "aa", "aaa", "aaaa"], sentence = "a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa"
 * 输出："a a a a a a a a bbb baba a"
 * 示例 4：
 *
 * 输入：dictionary = ["catt","cat","bat","rat"], sentence = "the cattle was rattled by the battery"
 * 输出："the cat was rat by the bat"
 * 示例 5：
 *
 * 输入：dictionary = ["ac","ab"], sentence = "it is abnormal that this solution is accepted"
 * 输出："it is ab that this solution is ac"
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/replace-words
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class ReplaceWordsTest {
    public static void main(String[] args) {
        List<String> dictionary = new ArrayList<>();
        dictionary.add("cat");
        dictionary.add("bat");
        dictionary.add("rat");
        String sentence = "the cattle was rattled by the battery";
//        replaceWords(dictionary, sentence);
        System.out.println(replaceWords1(dictionary, sentence));;
//        System.out.println(replaceWords2(dictionary, sentence));;
    }

    /**
     * 方法一：前缀哈希【通过】
     * 思路
     * 遍历句子中每个单词，查看单词前缀是否为词根。
     * 算法
     * 将所有词根 roots 存储到集合 Set 中。遍历所有单词，判断其前缀是否为词根。如果是，则使用前缀代替该单词；否则不改变该单词。
     * @param roots
     * @param sentence
     * @return
     */
    public static String replaceWords1(List<String> roots, String sentence) {
        Set<String> rootSet = new HashSet();
        for (String root: roots)
            rootSet.add(root);

        StringBuilder ans = new StringBuilder();
        for (String word: sentence.split("\\s+")) {
            String prefix = "";
            for (int i = 1; i <= word.length(); ++i) {
                prefix = word.substring(0, i);
                if (rootSet.contains(prefix)) break;
            }
            if (ans.length() > 0) ans.append(" ");
            ans.append(prefix);
        }
        return ans.toString();
    }
//
//    作者：LeetCode
//    链接：https://leetcode-cn.com/problems/replace-words/solution/dan-ci-ti-huan-by-leetcode/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

    public static String replaceWords(List<String> roots, String sentence) {
        Set<String> rootSet = new HashSet<>();
        for (String root:roots)
            rootSet.add(root);

        String newSentenceStr = "";
        for (String word : sentence.split("\\s+")) {
//            System.out.println("word="+word);
            for (int i=0; i<word.length();
                 i++) {


                if(word.length()>3){
                    System.out.println("word.substring(0, 3)="+word.substring(0, 3));
                    System.out.println("roots=" + word+", word="+word);
                    if(word.substring(0, 3).equals(word)){
                        word = word.substring(0, 3);
                        System.out.println("word=" + word);
                    }
//                    continue;
                }
                if(!word.equals(word)){
                    newSentenceStr += word+" ";
                }



//                String substrings = word.substring(0, 3);
//                System.out.println("substrings="+substrings);

//                if (substrings ==word) {
//
//                    substrings = word;
//
//                    newSentenceArr.concat(word);
//                }

            }


        }
        System.out.println("newSentenceStr=" + newSentenceStr);
        return newSentenceStr;
    }

    /**
     * 方法二：前缀树【通过】
     * 思路和算法
     *
     * 把所有的词根放入前缀树中，在树上查找每个单词的最短词根，该操作可在线性时间内完成。
     * @param roots
     * @param sentence
     * @return
     */
    public static String replaceWords2(List<String> roots, String sentence) {
        TrieNode trie = new TrieNode();
        for (String root: roots) {
            TrieNode cur = trie;
            for (char letter: root.toCharArray()) {
                if (cur.children[letter - 'a'] == null)
                    cur.children[letter - 'a'] = new TrieNode();
                cur = cur.children[letter - 'a'];
            }
            cur.word = root;
        }

        StringBuilder ans = new StringBuilder();

        for (String word: sentence.split("\\s+")) {
            if (ans.length() > 0)
                ans.append(" ");

            TrieNode cur = trie;
            for (char letter: word.toCharArray()) {
                if (cur.children[letter - 'a'] == null || cur.word != null)
                    break;
                cur = cur.children[letter - 'a'];
            }
            ans.append(cur.word != null ? cur.word : word);
        }
        return ans.toString();
    }
    static class TrieNode {
        TrieNode[] children;
        String word;
        TrieNode() {
            children = new TrieNode[26];
        }
    }
//    作者：LeetCode
//    链接：https://leetcode-cn.com/problems/replace-words/solution/dan-ci-ti-huan-by-leetcode/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。


}
